﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.Text;
using System.IO;

namespace MyLibrary.Data.Builder.Config
{
    /// <summary>
    /// 报表配置类
    /// </summary>
    [Serializable]
    public sealed class ReportConfig
    {
        private string _title = String.Empty;
        /// <summary>
        /// 报表标题
        /// </summary>
        [XmlElementAttribute("Title")]
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        private string _dbConnKey = String.Empty;
        /// <summary>
        /// 数据库配置键
        /// </summary>
        [XmlElementAttribute("DbConfigKey")]
        public string DbConnKey
        {
            get { return _dbConnKey; }
            set { _dbConnKey = value; }
        }

        private StatConditions _statConditionList = new StatConditions();
        /// <summary>
        /// 查询条件列表
        /// </summary>
        [XmlElementAttribute("StatConditions")]
        public StatConditions StatConditionList
        {
            get { return _statConditionList; }
            set { _statConditionList = value; }
        }

        private Labels _reportLabelList = new Labels();
        /// <summary>
        /// 报表标签集合
        /// </summary>
        [XmlElementAttribute("Labels")]
        public Labels ReportLabelList
        {
            get { return _reportLabelList; }
            set { _reportLabelList = value; }
        }
        /// <summary>
        /// 读取报表设置
        /// </summary>
        /// <param name="filePath">设置文件路径</param>
        /// <returns>报表设置</returns>
        public static ReportConfig LoadConfig(string filePath)
        {
            XmlSerializer xs = new XmlSerializer(typeof(ReportConfig));
            StreamReader sr = new StreamReader(filePath);
            ReportConfig rc = xs.Deserialize(sr) as ReportConfig;
            sr.Close();
            return rc;
        }

        public void SaveConfig(string file)
        {
            XmlSerializer xs = new XmlSerializer(typeof(ReportConfig));
            StreamWriter sw = new StreamWriter(file);
            xs.Serialize(sw, this);
            sw.Close();
        }
    }

    /// <summary>
    /// 统计条件
    /// </summary>
    [Serializable]
    public sealed class StatConditions
    {
        private ConditionList conditions = new ConditionList();
        [XmlElementAttribute("Condition")]
        public ConditionList Conditions
        {
            get { return conditions; }
            set { conditions = value; }
        }

        public static StatConditions LoadConfig(string filePath)
        {
            XmlSerializer xs = new XmlSerializer(typeof(StatConditions));
            StreamReader sr = new StreamReader(filePath);
            StatConditions rc = xs.Deserialize(sr) as StatConditions;
            sr.Close();
            return rc;
        }

        public void SaveConfig(string file)
        {
            XmlSerializer xs = new XmlSerializer(typeof(ReportConfig));
            StreamWriter sw = new StreamWriter(file);
            xs.Serialize(sw, this);
            sw.Close();
        }
    }

    /// <summary>
    /// 报表标签
    /// </summary>
    [Serializable]
    public class Labels
    {
        private ReportLabelList lables = new ReportLabelList();
        [XmlElementAttribute("Label")]
        public ReportLabelList Lables
        {
            get { return lables; }
            set { lables = value; }
        }

    }
    
    /// <summary>
    /// 报表的标题头
    /// </summary>
    [Serializable]
    public class Columns
    {
        private ColumnsList columns = new ColumnsList();
        [XmlElementAttribute("Column")]
        public ColumnsList ColumnsList
        {
            get { return columns; }
            set { columns = value; }
        }
    }
}
